﻿namespace Typps
{
    using System;

    using Wilco.CodeHilighter.Engine.SyntaxHighlighting;

    //ToDO: ShowLanguageName is never used, remove perhaps?
    public class SyntaxHighlighter
    {
        #region Fields

        const string lineContentCss = " style=\"float:left;\"";

        bool _allowLineNumbering;
        string _language = "#C";
        IParser _parser;
        bool _showLanguageName;
        string _sourceCode;

        #endregion Fields

        #region Constructors

        public SyntaxHighlighter(string language)
            : this(language, new HtmlParser())
        {
        }

        public SyntaxHighlighter(string language, IParser parser)
            : this(language, parser, false, null)
        {
        }

        public SyntaxHighlighter(string language, 
            IParser parser, string sourceCode)
            : this(language,parser,false,sourceCode)
        {
        }

        public SyntaxHighlighter(string language,
            IParser parser,bool allowLineNumbering,string sourceCode)
        {
            _language = language;
            _parser = parser;
            _allowLineNumbering = allowLineNumbering;
            _sourceCode = sourceCode;
        }

        #endregion Constructors

        #region Properties

        public bool AllowLineNumbering
        {
            get { return _allowLineNumbering; }
            set { _allowLineNumbering = value; }
        }

        public string Language
        {
            get { return _language; }
            set { _language = value; }
        }

        public IParser Parser
        {
            get { return _parser; }
            set { _parser = value; }
        }

        public bool ShowLanguageName
        {
            get { return _showLanguageName; }
            set { _showLanguageName = value; }
        }

        public string SourceCode
        {
            get { return _sourceCode; }
            set { _sourceCode = value; }
        }

        #endregion Properties

        #region Methods

        public string GetParsedSourceCode()
        {
            if (string.IsNullOrEmpty(SourceCode)) return string.Empty;
            return GetParsedSourceCode(SourceCode, false);
        }

        public string GetParsedSourceCode(bool inlineCss)
        {
            if (string.IsNullOrEmpty(SourceCode)) return string.Empty;
            return GetParsedSourceCode(SourceCode, inlineCss);
        }

        public string GetParsedSourceCode(string source, bool inlineCss)
        {
            Register register = Register.Instance;
            HighlighterBase highlighter = register.Highlighters[Language];
            highlighter.Parser = Parser;
            source = highlighter.Parse(source);
            if (AllowLineNumbering)
            {
                if (Parser is HtmlParser)
                {
                    HtmlParser p = (HtmlParser)Parser;
                    source = p.CreateLineNumbering(source, inlineCss);
                }
            }
            else
            {
                string css1 = string.Empty;
                if (inlineCss)
                    css1 = lineContentCss;
                source = string.Format(
            @"<div class=""linecontent""{0}>
                <pre>{1}</pre>
            </div>", css1, source);
            }
            return source;
        }

        #endregion Methods
    }
}